package com.lastproject.web.bean;

import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.lastproject.dto.QuestionDTO;
import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.dto.SurveyDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.QuestionDAORemote;
import com.lastproject.ejb.dao.QuestionOptionDAORemote;
import com.lastproject.ejb.dao.SurveyDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

///**
// * Session Bean implementation class AnswerStatsBean
// */
@ManagedBean(name = "surveyStatsBean")
@RequestScoped
public class SurveyStatsBean {

//	@ManagedProperty(value = "#{param.idSurv}")
    private int idSurv;
	
	private String status;
	
	private List<QuestionDTO> questionDtoList;
		

	@ManagedProperty(value = "#{questionOptionDtoList}")
	private List<QuestionOptionDTO> questionOptionDtoList;
	
	@ManagedProperty(value = "#{surveyDTO}")
	private SurveyDTO surveyDTO;
	@EJB
	SurveyDAORemote surveyDao;

	
	@ManagedProperty(value = "#{questionDTO}")
	private QuestionDTO questionDTO;
	@EJB
	QuestionDAORemote questionDao;

	@ManagedProperty(value = "#{questionOptionDTO}")
	private QuestionOptionDTO questionOptionDTO;
	@EJB
	QuestionOptionDAORemote questionOptionDao;
	
	@EJB
	UserDAORemote userDaor;
	
	private UserDTO userDTO;


	public SurveyStatsBean() {

	}
	
	@PostConstruct
	public void postConstr() {
//		questionDTO = questionDao.findById(idSurv);
		try{
			int idSurv = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idSurv").toString());
			this.idSurv = idSurv;
//			System.out.println("SurveyStatBean, id of survey: " + idSurv);
			surveyDTO = surveyDao.findById(idSurv);
			this.setSurveyInfo(idSurv);
		} catch(NullPointerException e){
		}
		
//		questionDTO = questionDao.findById(idSurv);
//		this.idSurv = idSurvey;
		
	}
	

	
	

	public SurveyDTO getSurveyDTO() {
		return surveyDTO;
	}

	public void setSurveyDTO(SurveyDTO surveyDTO) {
		this.surveyDTO = surveyDTO;
	}

	

	public QuestionDTO getQuestionDTO() {
		return questionDTO;
	}

	public void setQuestionDTO(QuestionDTO questionDTO) {
		this.questionDTO = questionDTO;
	}

	public QuestionOptionDTO getQuestionOptionDTO() {
		return questionOptionDTO;
	}

	public void setQuestionOptionDTO(QuestionOptionDTO questionOptionDTO) {
		this.questionOptionDTO = questionOptionDTO;
	}

	
	
	public SurveyStatsBean(int idSurv) {
		this.idSurv = idSurv;
	}
	
	

	public int getIdSurv() {
		return idSurv;
	}

	public void setIdSurv(int idSurv) {
		this.idSurv = idSurv;
//		this.getSurveyInfo(idSurv);
	}
	
	public void setSurveyInfo(int idSurv) {
		this.setSurveyDTO(surveyDao.findById(idSurv));
		this.setUserDTO(userDaor.findCreatorUserBySurvey(idSurv));
		
//		System.out.println("SurveyStatsBean user ID: " + this.getSurveyDTO().getIdUser());
//		System.out.println("SurveyStatsBean topic ID: " + this.getSurveyDTO().getIdTopic());
//		System.out.println("SurveyStatsBean ID: " + this.getSurveyDTO().getId());
//		System.out.println("SurveyStatsBean title: " + this.getSurveyDTO().getTitle());
//		System.out.println("SurveyStatsBean title: " + this.getSurveyDTO().getDescription());
//		System.out.println("SurveyStatsBean start date: " + this.getSurveyDTO().getStartDate().toString());
//		System.out.println("SurveyStatsBean end date: " + this.getSurveyDTO().getEndDate().toString());
//		System.out.println("SurveyStatsBean creator: " + this.getUserDTO().getFirstname()+" "+this.getUserDTO().getLastname());
		
	}
	
//	public List<QuestionDTO> getQuestionListForSurvey(int id) {
//		List<QuestionDTO> questionDtoList = null;
//		questionDtoList = questionDao.getQuestionListBySurveyId(id);
//		return questionDtoList;
//	}
	
	
	public String getStatus() {
		Calendar calStart = Calendar.getInstance();
		Calendar calEnd = Calendar.getInstance();
		
		Calendar calToday = Calendar.getInstance();
		try{
		calStart.setTime(this.getSurveyDTO().getStartDate());
		calEnd.setTime(this.getSurveyDTO().getEndDate());
		
		if (calStart.after(calToday)) {
			status = "not yet available";
		} else if (calEnd.after(calToday) || calEnd.equals(calToday)) {
			status = "ongoing";
		} else if (calEnd.before(calToday)) {
			status = "completed";
		}
		} catch(NullPointerException e){
			
		}
		
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<QuestionDTO> getQuestionDtoList() {
		return questionDtoList;
	}

	public void setQuestionDtoList(List<QuestionDTO> questionDtoList) {
		this.questionDtoList = questionDtoList;
	}

	public List<QuestionOptionDTO> getQuestionOptionDtoList() {
		return questionOptionDtoList;
	}

	public void setQuestionOptionDtoList(List<QuestionOptionDTO> questionOptionDtoList) {
		this.questionOptionDtoList = questionOptionDtoList;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	
	

}
